package com.dengbw.systemtransfer.controller;

import com.dengbw.systemtransfer.model.DepInfo;
import com.dengbw.systemtransfer.model.Result;
import com.dengbw.systemtransfer.model.SysCorpVo;
import com.dengbw.systemtransfer.model.request.MultiTransfer;
import com.dengbw.systemtransfer.service.DingCommonService;
import com.dengbw.systemtransfer.service.QueryService;
import com.dengbw.systemtransfer.service.SysUserService;
import com.dengbw.systemtransfer.service.TransferService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.Map;

/**
 * @author dengbw
 * @date 2021/11/2 2:48 下午
 */
@Api(tags = "迁移")
@RestController
@RequestMapping("transfer")
public class TransferController {

    TransferService transferService;
    SysUserService sysUserService;
    QueryService queryService;
    DingCommonService dingCommonService;

    @Autowired
    public TransferController(TransferService transferService, SysUserService sysUserService, QueryService queryService, DingCommonService dingCommonService) {
        this.transferService = transferService;
        this.sysUserService = sysUserService;
        this.queryService = queryService;
        this.dingCommonService = dingCommonService;
    }

    @ApiOperation(value = "迁移村", notes = "可以向一个镇迁移单个或多个村，也可以分别向多个镇迁移多个村")
    @PostMapping()
    public Result<Map<String, String>> transferBatch (@RequestBody MultiTransfer multi) {
        return Result.success(transferService.transferBatch(multi));
    }

    @ApiOperation(value = "更新用户信息", notes = "在迁移并完成同步后调用")
    @PostMapping("user/update")
    public Result<Integer> updateUser(@RequestBody List<Long> subIds) {
        return Result.success(sysUserService.updateUser(subIds));
    }

    @ApiOperation(value = "查询组织信息", notes = "村或镇都可以查, 在数据库中查询")
    @GetMapping("org/{name}")
    public Result<List<SysCorpVo>> queryOrg(@PathVariable String name) {
        return Result.success(queryService.queryCorp(name));
    }

    @ApiOperation(value = "查询子部门信息", notes = "在钉钉中查询")
    @GetMapping("sub/{appkey}/{appsecret}")
    public Result<DepInfo> queryDepInfo(@PathVariable("appkey") String appKey,
                                        @PathVariable("appsecret") String appSecret) {
        return Result.success(dingCommonService.fetchDepInfo(appKey, appSecret));
    }
}
